package org.soderiakelvy.controller.informe;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.soderiakelvy.model.domain.TipoProductoVO;
import org.soderiakelvy.model.service.ProductoServicio;
import org.soderiakelvy.model.service.imp.ProductoServicioImp;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.export.JRPdfExporter;

@ManagedBean
@SessionScoped
public class InformeRepartos {
	
	
	public InformeRepartos(){
	
	}	
		
	public String printInformeRepartos(){	
		System.out.println("printInformeRepartos()");
		
		try {
			
			
			String path = "/informes/totalEmpT.jrxml";
//			String path = "/informes/totalEmpT.jasper";
			System.out.println("carga el path");
			InputStream jasperTemplate = FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream(path);
			System.out.println("jaspertempalte");
			JasperReport jasperReport = JasperCompileManager.compileReport(jasperTemplate);
			System.out.println("jasper reprot");
			Map parameters = new HashMap();			

			String nombre = "ricardo";	
			/********************************************************************************/
			parameters.put("nombre", nombre);
			/********************************************************************************/
			MysqlDataSource dataSource = new MysqlDataSource();
			
			dataSource.setUser("root");
			dataSource.setPassword("toor");
			dataSource.setDatabaseName("soderia_test");
			dataSource.setServerName("localhost");

			Connection conexion = (Connection) dataSource.getConnection();
//		    Connection conn = this.getDataSource().getConnection();		    
//		    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, conn);
//		    sun.jdbc.odbc.ee.DataSource mio = new sun.jdbc.odbc.ee.DataSource()
//		    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters);
//			JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, new JREmptyDataSource());
			JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, conexion);
			
			System.out.println("jasper print");
			conexion.close();
//		    conn.close();
//			System.out.println("entes de responde");		
//			HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
//			response.setContentType("application/pdf");
//			response.setHeader("Content-Disposition","inline; filename=\"BEGU-Informes.pdf\"");
//			System.out.println("despues de response");
//			ServletOutputStream outputStream = response.getOutputStream();
//			JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
//			System.out.println("despues de flush");
//			outputStream.flush();
//			System.out.println("flush");
//			outputStream.close();
//			System.out.println("close");
//			FacesContext.getCurrentInstance().renderResponse();
//			FacesContext.getCurrentInstance().responseComplete();
			
			try {
				FacesContext facesContext = FacesContext.getCurrentInstance();
				facesContext.responseComplete();
				ServletContext scontext = (ServletContext) facesContext.getExternalContext().getContext();
//				JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, new JREmptyDataSource());
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				JRPdfExporter exporter = new JRPdfExporter();
				exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
				exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
				exporter.exportReport();
				byte[] bytes = baos.toByteArray();
				if (bytes != null && bytes.length > 0) {
					HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
					response.setContentType("application/pdf");
					response.setHeader("Content-disposition","inline; filename=\"relatorioPorData.pdf\"");
					response.setContentLength(bytes.length);
					ServletOutputStream outputStream = response.getOutputStream();
					outputStream.write(bytes, 0, bytes.length);
					outputStream.flush();
					outputStream.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}

		} catch (JRException e) {			
//		} catch (IOException e) {			
		} catch (Exception e) {			
		}
		
		System.out.println("end -- printInformeRepartos()");
		return null;
	}
	
//	public void nada() {		
//		try {
//			FacesContext facesContext = FacesContext.getCurrentInstance();
//			facesContext.responseComplete();
//			ServletContext scontext = (ServletContext) facesContext.getExternalContext().getContext();
//			JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, new JREmptyDataSource());
//			ByteArrayOutputStream baos = new ByteArrayOutputStream();
//			JRPdfExporter exporter = new JRPdfExporter();
//			exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
//			exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
//			exporter.exportReport();
//			byte[] bytes = baos.toByteArray();
//			if (bytes != null && bytes.length > 0) {
//				HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
//				response.setContentType("application/pdf");
//				response.setHeader("Content-disposition","inline; filename=\"relatorioPorData.pdf\"");
//				response.setContentLength(bytes.length);
//				ServletOutputStream outputStream = response.getOutputStream();
//				outputStream.write(bytes, 0, bytes.length);
//				outputStream.flush();
//				outputStream.close();
//			}
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}
	
}
